package com.myandroid.sharedpreferences;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MySharedPreferencesActivity extends Activity {
    /** Called when the activity is first created. */
	
	private EditText	privateText;
	private EditText	sharedText;
	private Button		saveButton;
	private Button		launchActivityButton;
	
	private SharedPreferences	prvPref;
	private SharedPreferences	sharedPref;
	
	private SharedPreferences.Editor	prvEditor;
	private SharedPreferences.Editor	sharedEditor;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    	prvPref		= getPreferences(Activity.MODE_PRIVATE);
    	sharedPref	= getSharedPreferences("pref", Activity.MODE_PRIVATE);
    	
    	prvEditor	= prvPref.edit();
    	sharedEditor= sharedPref.edit();
    	
        saveButton	= (Button)findViewById(R.id.saveButton);
        saveButton.setOnClickListener(new OnClickListener() {
        	@Override
            public void onClick(View v) {
            	String prvValue		= privateText.getText().toString();
            	String sharedValue	= sharedText.getText().toString();

            	prvEditor.putString("value", prvValue);
            	sharedEditor.putString("value", sharedValue);
            	
            	prvEditor.commit();
            	sharedEditor.commit();
            	Toast.makeText(MySharedPreferencesActivity.this, "Saved", Toast.LENGTH_SHORT)
            	.show();    	
            }   
        });
        
        launchActivityButton = (Button)findViewById(R.id.launchActivity2);
        launchActivityButton.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		// Intent intent = new Intent(CreateActivity.this, NewActivity.class);
        		Intent intent = new Intent("com.androidhuman.action.Activity2");
        		//startActivity(intent);
        		startActivityForResult(intent,0);
        	}  	
        });
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	String prvValue		= prvPref.getString("value", "");
    	String sharedValue	= sharedPref.getString("value", "");
    	
    	privateText.setText(String.valueOf(prvValue));
    	sharedText.setText(String.valueOf(sharedValue));    	
    }
}